home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / RTGMaster / demos / flame / flame.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-24  |  7.8 KB  |  320 lines

  1. /*
  2.         Flame
  3.  
  4.         A demo of rtg.library
  5.  
  6.         Written by Thomas & Hans-Jörg Frieden
  7.                    Schlossstr. 176
  8.                    54293 Trier
  9.                    tfrieden@fax.uni-trier.de
  10.                    hfrieden@fix.uni-trier.de
  11.  
  12. */
  13. //* "Includes"
  14. #include <stdlib.h>
  15. #include <proto/utility.h>
  16. #include <proto/exec.h>
  17. #include <clib/rtgmaster_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/utility_protos.h>
  20. #include <pragmas/rtgmaster_pragmas.h>
  21. #include <pragmas/exec_pragmas.h>
  22. #include <pragmas/utility_pragmas.h>
  23. #include <exec/memory.h>
  24. #include <utility/tagitem.h>
  25. #include <rtgmaster/rtgmaster.h>
  26. #include <rtgmaster/rtgsublibs.h>
  27. #include <rtgmaster/rtgc2p.h>
  28. #include <proto/rtgmaster.h>
  29. #include "timer.h"
  30. #include <string.h>
  31. #include <stdio.h>
  32.  
  33. int currentbuffer=0;
  34.  
  35. //*
  36.  
  37. //* "Defines"
  38. #define XSIZE 80
  39. #define YSIZE 65
  40. #define YSCRN 60
  41. #define MSIZE (XSIZE*YSIZE)
  42. #define DECAY 3
  43. #define CBUF    76800
  44. //*
  45. //* "Pragmas"
  46. extern void __asm CopyFrame(register __a0 UBYTE *adr, 
  47.                             register __d0 ULONG size);
  48. extern void __asm CopyFrame2(register __a0 UBYTE *adr, 
  49.                              register __d0 ULONG aize);
  50. extern void __asm DrawMeter(register __a0 APTR buf, 
  51.                             register __d0 ULONG fps);
  52.  
  53. UBYTE MouseButton(void);
  54. //*
  55. //* "Globals"
  56. struct RtgScreen *RtgScreen;
  57. struct ScreenReq *sr;
  58. struct RTGMasterBase *RTGMasterBase;
  59. struct Library *UtilityBase;
  60. struct TagItem rtag[] = {
  61.     smr_MinWidth,       320,
  62.     smr_MinHeight,      200,
  63.     smr_MaxWidth,       1024,
  64.     smr_MaxHeight,      768,
  65.     smr_ChunkySupport,  512,
  66.     smr_PlanarSupport,  -1,
  67.     smr_Buffers,2,
  68.     TAG_DONE,           NULL
  69. };
  70.  
  71. struct TagItem gtag[] = {
  72.     grd_BytesPerRow,    0,
  73.     grd_Width,          0,
  74.     grd_Height,         0,
  75.     grd_Depth,          0,
  76.     grd_PixelLayout,    0,
  77.     grd_ColorSpace,     0,
  78.     grd_PlaneSize,      0,
  79.     TAG_DONE,           0
  80. };
  81.  
  82. struct TagItem tacks[] = {
  83.     rtg_Buffers,2,
  84.     rtg_Workbench,0,
  85.     TAG_DONE,0
  86. };
  87.  
  88. UBYTE *sc1;
  89. UBYTE scrn[MSIZE];
  90. ULONG cmap[800];
  91. UBYTE blah;
  92. BOOL Planar;
  93. UBYTE *cbuf=NULL;
  94. ULONG width;
  95. ULONG height;
  96. UBYTE *sadr;
  97. ULONG num,avg;
  98. ULONG c2psignal;
  99. //*
  100. //* "NewFrame"
  101. void NewFrame(void) {
  102.     int i,x;
  103.     UBYTE *a;
  104.  
  105.     extern void GenFrame(void);
  106.  
  107.     a=&scrn[MSIZE-1]; i=XSIZE;
  108.  
  109.     while (i>0) {
  110.         x=rand();
  111.         if (x>RAND_MAX/2) *a=255;
  112.         else *a=0;
  113.         i--; a--;
  114.     }
  115.  
  116.     i=MSIZE-XSIZE-1;
  117.     a=scrn;
  118.  
  119.     GenFrame();
  120. }
  121. //*
  122. //* "DrawFrame"
  123. void DrawFrame(struct RtgScreen *s, ULONG size, ULONG ticks) {
  124.  
  125.     if (blah==0) CopyFrame(cbuf, size);
  126.     else CopyFrame2(cbuf,size);
  127.     DrawMeter(cbuf, ticks);
  128.     //CopyRtgPixelArray(RtgScreen,sadr,cbuf,0,0,320,200,0,0);
  129.     CopyRtgBlit(RtgScreen,sadr,cbuf,0,0,0,width,height,width,height,0,0);
  130. }
  131. //*
  132. //* "DrawFrameP"
  133. void DrawFrameP(struct RtgScreen *s, ULONG size, ULONG ticks) {
  134.     UBYTE *adr;
  135.  
  136.     adr=cbuf;                               // Draw into chunky buffer first
  137.     if (blah==0) CopyFrame(adr, 320);
  138.     else CopyFrame2(adr,320);
  139.     DrawMeter(adr, ticks);
  140.     adr = sadr;
  141.     CallRtgC2P(s,GetBufAdr(RtgScreen,1-currentbuffer),cbuf,c2psignal,0,0,width,height,c2p_Selected);
  142.     SwitchScreens(RtgScreen,1-currentbuffer);
  143.     currentbuffer=1-currentbuffer;
  144. }
  145. //*
  146. //* "fail"
  147. void fail(void) {
  148.     if (RtgScreen) CloseRtgScreen(RtgScreen);
  149.     if (RTGMasterBase) CloseLibrary((struct Library *)RTGMasterBase);
  150.     if (UtilityBase) CloseLibrary(UtilityBase);
  151.     if (cbuf) FreeMem(cbuf, CBUF);
  152.     exit(0L);
  153. }
  154. //*
  155. //* "main"
  156. void main(int argc, char **argv) {
  157.    /*
  158.     * Since this is a demo, I don't check anything at all
  159.     * and simply assume that every open went ok... 8-)
  160.     */
  161.     int i, x;
  162.     struct TagItem *tag;
  163.     UBYTE rr, rg, rb;
  164.     ULONG size;
  165.     ULONG ticks=0, max=0, min=300;
  166.  
  167.     if (argc>1 && stricmp(argv[1],"small")==0) blah=1; else blah=0;
  168.     if (argc>2 && stricmp(argv[2],"window")==0) tacks[1].ti_Data=LUT8;
  169.     if (argc>1 && stricmp(argv[1],"window")==0) tacks[1].ti_Data=LUT8;
  170.         if (InitTimer()==FALSE) {
  171.         printf("Unable to open timer device\n");
  172.         fail();
  173.     }
  174.  
  175.     RTGMasterBase = (struct RTGMasterBase *)OpenLibrary((STRPTR)"rtgmaster.library", 0);
  176.     UtilityBase = OpenLibrary((STRPTR)"utility.library", 37L);
  177.     c2psignal=AllocSignal(-1);
  178.     sr = RtgScreenModeReq(rtag);
  179.  
  180.     if (sr==NULL) fail();
  181.     if (c2psignal==-1) fail();
  182.     RtgScreen = OpenRtgScreen(sr, tacks);
  183.  
  184.     GetRtgScreenData(RtgScreen, gtag);
  185.  
  186.     tag=FindTagItem(grd_BytesPerRow, gtag);
  187.     size = tag->ti_Data;
  188.  
  189.     tag=FindTagItem(grd_Width, gtag);
  190.     width = tag->ti_Data;
  191.  
  192.     tag=FindTagItem(grd_Height,gtag);
  193.     height = tag->ti_Data;
  194.  
  195.     tag=FindTagItem(grd_PixelLayout, gtag);
  196.     if (tag->ti_Data != grd_PLANAR && tag->ti_Data != grd_CHUNKY) {
  197.         printf("Screenmode not supported\n");
  198.         fail();
  199.     }
  200.  
  201.     if (tag->ti_Data == grd_PLANAR) Planar = TRUE;
  202.     else Planar = FALSE;
  203.  
  204.     printf("Screen is %ld x %ld x %ld\n", gtag[1].ti_Data, gtag[2].ti_Data, gtag[3].ti_Data);
  205.     printf("It has %ld bytes per row", size);
  206.     if (size>gtag[1].ti_Data) {
  207.         printf(", which means that the screen is wider than what you see\n");
  208.     } else printf("\n");
  209.  
  210.         cbuf = AllocMem(CBUF, MEMF_CLEAR|MEMF_FAST);
  211.         if (cbuf==NULL) {
  212.             cbuf=AllocMem(CBUF, MEMF_CLEAR);
  213.             if (cbuf==NULL) {
  214.                 printf("Out of memory *SIGH*\n");
  215.                 fail();
  216.             }
  217.         }
  218.  
  219. if (gtag[3].ti_Data==8)
  220. {
  221.     cmap[0] = 256 * 65536;
  222.     rr = 0;
  223.     rg = 0;
  224.     rb = 0;
  225.     x = 1;
  226.     for (i = 0; i < 64; i++) {
  227.         cmap[x++] = rr * 0x1111111;
  228.         cmap[x++] = rg * 0x1111111;
  229.         cmap[x++] = rb * 0x1111111;
  230.         rr += 3;
  231.     }
  232.     for (i = 0; i < 127; i++) {
  233.         cmap[x++] = rr * 0x1111111;
  234.         cmap[x++] = rg * 0x1111111;
  235.         cmap[x++] = rb * 0x1111111;
  236.         rg += 3;
  237.     }
  238.     for (i = 0; i < 60; i++) {
  239.         cmap[x++] = rr * 0x1111111;
  240.         cmap[x++] = rg * 0x1111111;
  241.         cmap[x++] = rb * 0x1111111;
  242.         rb += 3;
  243.     }
  244.     for (i = 0; i < 4; i++) {
  245.         cmap[x++]=0xFFFFFFFF;
  246.         cmap[x++]=0xFFFFFFFF;
  247.         cmap[x++]=0xFFFFFFFF;
  248.     }
  249.     cmap[x++]=0;
  250. }
  251. else
  252. {
  253.    cmap[0] = 64 * 65536;
  254.     rr = 0;
  255.     rg = 0;
  256.     rb = 0;
  257.     x = 1;
  258.     for (i = 0; i < 16; i++) {
  259.         cmap[x++] = rr * 0x1111111;
  260.         cmap[x++] = rg * 0x1111111;
  261.         cmap[x++] = rb * 0x1111111;
  262.         rr += 12;
  263.     }
  264.     for (i = 0; i < 31; i++) {
  265.         cmap[x++] = rr * 0x1111111;
  266.         cmap[x++] = rg * 0x1111111;
  267.         cmap[x++] = rb * 0x1111111;
  268.         rg += 12;
  269.     }
  270.     for (i = 0; i < 12; i++) {
  271.         cmap[x++] = rr * 0x1111111;
  272.         cmap[x++] = rg * 0x1111111;
  273.         cmap[x++] = rb * 0x1111111;
  274.         rb += 12;
  275.     }
  276.     for (i = 0; i < 4; i++) {
  277.         cmap[x++]=0xFFFFFFFF;
  278.         cmap[x++]=0xFFFFFFFF;
  279.         cmap[x++]=0xFFFFFFFF;
  280.     }
  281.     cmap[x++]=0;
  282. }
  283.     LockRtgScreen(RtgScreen);
  284.     LoadRGBRtg(RtgScreen, (APTR) cmap);
  285.     UnlockRtgScreen(RtgScreen);
  286.  
  287.     if (RtgScreen) {
  288.         LockRtgScreen(RtgScreen);
  289.         sadr = (UBYTE *)GetBufAdr(RtgScreen,0);
  290.  
  291.         num=0;
  292.         while(MouseButton()==0) {
  293.             StartClock();
  294.             NewFrame();
  295.             if (Planar==FALSE) DrawFrame(RtgScreen, size, ticks);
  296.             else DrawFrameP(RtgScreen, size, ticks);
  297.             ticks=StopClock();
  298.             num++;
  299.             avg+=ticks;
  300.             if (max<ticks) max=ticks;
  301.             if (min>ticks) min=ticks;
  302.         }
  303.         UnlockRtgScreen(RtgScreen);
  304.         CloseRtgScreen(RtgScreen);
  305.     }
  306.     if (max==0) max=1;
  307.     if (min==0) min=1;
  308.     printf("Min Frame rate: %ld\n", max);
  309.     printf("Max Frame rate: %ld\n", min);
  310.     printf("Avg Frame rate: %ld\n", avg/num);
  311.     CloseTimer();
  312.     FreeSignal(c2psignal);
  313.     FreeMem(cbuf,CBUF);
  314.     FreeRtgScreenModeReq(sr);
  315.     CloseLibrary((struct Library *)RTGMasterBase);
  316.     CloseLibrary(UtilityBase);
  317. }
  318. //*
  319.  
  320.